package com.example.mode.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.mode.R;

import java.util.ArrayList;
import java.util.List;

public class NearbyFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private TextView wordTextView;
    private TextView meaningTextView;
    private Button nextButton;

    private List<String[]> wordList;
    private int currentIndex = 0;

    public NearbyFragment() {
        // Required empty public constructor
    }

    public static NearbyFragment newInstance(String param1, String param2) {
        NearbyFragment fragment = new NearbyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        initWordList();
    }

    private void initWordList() {
        wordList = new ArrayList<>();
        wordList.add(new String[]{"apple", "苹果"});
        wordList.add(new String[]{"banana", "香蕉"});
        wordList.add(new String[]{"cherry", "樱桃"});
        wordList.add(new String[]{"pinch", "捏"});
        wordList.add(new String[]{"gauge", "测量仪"});
        wordList.add(new String[]{"rumour", "谣言"});
        wordList.add(new String[]{"position", "位置"});
        wordList.add(new String[]{"personally", "个人地"});
        wordList.add(new String[]{"manufacture", "生产"});
        wordList.add(new String[]{"consumption", "消耗"});

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nearby, container, false);
        wordTextView = view.findViewById(R.id.word_textview);
        meaningTextView = view.findViewById(R.id.meaning_textview);
        nextButton = view.findViewById(R.id.next_button);

        showCurrentWord();

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex++;
                if (currentIndex >= wordList.size()) {
                    currentIndex = 0;
                }
                showCurrentWord();
            }
        });

        return view;
    }

    private void showCurrentWord() {
        String[] currentWord = wordList.get(currentIndex);
        wordTextView.setText(currentWord[0]);
        meaningTextView.setText(currentWord[1]);
    }
}